<?php

namespace App\Http\Controllers\Admin;

use App\Models\MetroStation;
use App\Services\Admin\MetroService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MetroStationController extends Controller
{
    protected $metroService;

    public function __construct(MetroService $metroService)
    {
        $this->metroService = $metroService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($id)
    {
        return view('admin.metro_station.index',compact('id'));
    }

    public function data(Request $request,$id)
    {
        $model = MetroStation::query();
        $res = $model->where('metro_id',$id)->orderBy('sort','asc')->paginate($request->get('limit',30))->toArray();
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id)
    {
        $metro = $this->metroService->getMetroById($id);
        return view('admin.metro_station.create',compact('metro'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request,[
            'name'  => 'required|string'
        ]);
        $data = $request->all();
        $map = explode(',',$data['map']);
        $data['longitude'] = $map[0];
        $data['latitude'] = $map[1];
        $metro_station = MetroStation::create($data);
        if ($metro_station){
            return redirect("/admin/metro_station/{$data['metro_id']}")->with(['status'=>'添加成功']);
        }
        return redirect("/admin/metro_station/{$data['metro_id']}")->withErrors(['status'=>'系统错误']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $metro_station = MetroStation::findOrFail($id);
        $metro = $metro_station->metro;
        if (!$metro_station){
            return redirect(route('admin.metro_station'))->withErrors(['status'=>'地铁线不存在']);
        }
        return view('admin.metro_station.edit',compact('metro','metro_station'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'name'  => 'required|string'
        ]);
        $metro_station = MetroStation::findOrFail($id);
        $data = $request->all();
        $map = explode(',',$data['map']);
        $data['longitude'] = $map[0];
        $data['latitude'] = $map[1];

        if ($metro_station->update($data)){
            return redirect("/admin/metro_station/{$data['metro_id']}")->with(['status'=>'更新成功']);
        }
        return redirect("/admin/metro_station/{$data['metro_id']}")->withErrors(['status'=>'系统错误']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        foreach (MetroStation::whereIn('id',$ids)->get() as $model){
            //删除
            $model->delete();
        }
        return response()->json(['code'=>0,'msg'=>'删除成功']);
    }

    public function getStationByMetroId(Request $request)
    {
        return $this->metroService->getStationByMetroId($request->input('metro_id'));
    }
}
